//
//  BaseTableViewController.swift
//  SinaMicroBlog
//
//  Created by wxy on 15/9/2.
//  Copyright © 2015年 wxy. All rights reserved.
//

import UIKit

class BaseTableViewController: UITableViewController {

    // 用户登陆标记
    //var userLogin = false
    var userLogin = UserAccountViewModel.sharedUserAccount.userLogin
    
    //用户登陆视图（为什么这里不使用懒加载）每个控制器各自拥有自己的 visitorView
    //提示：如果使用懒加载，会在用户登录成功之后，视图仍然被创建，虽然不会影响程序执行，但是会消耗内存
    var vistorView: VistorLoginView?
//    lazy var vistorView: VistorLoginView = VistorLoginView()
    
    override func loadView() {
        
        userLogin ? super.loadView() : setUpVistorView()
    }
    
    
    //设置访客视图
    private func setUpVistorView() {
        
        vistorView = VistorLoginView()
        
        //替换根视图
        view = vistorView
        
        //设置导航栏按钮并添加点击事件
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: UIBarButtonItemStyle.Plain, target: self, action: "vistorLoginViewWillRegister")
        
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登陆", style: UIBarButtonItemStyle.Plain, target: self, action: "vistorLoginViewWillLogin")
     
        //监听首页按钮点击
        vistorView?.registerButton.addTarget(self, action: "vistorLoginViewWillRegister", forControlEvents: UIControlEvents.TouchUpInside)
        
        vistorView?.loginButton.addTarget(self, action: "vistorLoginViewWillLogin", forControlEvents: UIControlEvents.TouchUpInside)
    }
    
    @objc private func vistorLoginViewWillRegister() {
        
        printLog("注册")
    }
    
    @objc private func vistorLoginViewWillLogin() {
        
        let nav = UINavigationController(rootViewController: OAuthViewController())
        
        presentViewController(nav, animated: true, completion: nil)
        
        
    }
    
}
